//
//  PriceViewController.m
//  A段项目
//
//  Created by dlios on 15/7/17.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "PriceViewController.h"
#import "BaseHeader.h"
#import "SANetWorkingTask.h"
#import "MJRefresh.h"
#import "PriceOffTableViewCell.h"
#import "RecSecViewController.h"
#import "PriceCarViewController.h"


@interface PriceViewController ()<UITableViewDataSource, UITableViewDelegate, PriceCarViewControllerDelegate>

@property (nonatomic, retain) UIButton *category;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic,assign) NSInteger detailsPageCount;
@property (nonatomic, retain) UILabel *car;
@property (nonatomic, retain) NSString *serialid;


@end

@implementation PriceViewController

- (void)dealloc
{
    [_category release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
        self.detailsPageCount = 2;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self createViews];
    // Do any additional setup after loading the view.
}

- (void)sendStr:(NSString *)str serialid:(NSString *)serialid
{
    self.serialid = serialid;
    self.car.text = str ;
    NSString *url = @"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=1&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=87dea7fece47586956c66e9e8518a2d3";
    if ([str isEqualToString:@"全部车型"]) {
        
    } else {
        url = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=1&pageindex=1&pagesize=20&skip=1&serialid=%@&carid=&token=4eebe67a933d56cdc09602bd89d4dd07", serialid];
    }
    [SANetWorkingTask request:url parmater:nil block:^(NSDictionary *result) {
        [_array removeAllObjects];
        NSDictionary *big = [result objectForKey:@"data"];
        NSArray *arr = [big objectForKey:@"list"];
        [_array addObjectsFromArray:arr];
        [_table reloadData];
        [_table.header beginRefreshing];
        
    }];

    
}

- (void)createViews
{
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
    top.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.6];
    [self.view addSubview:top];
    [top release];
    
    UIButton *carKind = [[UIButton alloc] initWithFrame:CGRectMake(0 , 0, 115, 30)];
    self.car = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 30)];
    _car.text = @"全部车型";
    _car.textAlignment = NSTextAlignmentCenter;
    _car.font = [UIFont systemFontOfSize:13];
    [carKind addSubview:_car];
    [_car release];

    [carKind setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
    carKind.imageEdgeInsets = UIEdgeInsetsMake(0, 95, 0, 0);
    [carKind addTarget:self action:@selector(carKind:) forControlEvents:UIControlEventTouchUpInside];
    [top addSubview:carKind];
    [carKind release];
    
//    UIButton *city = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth / 3 , 0, 100, 30)];
//    [city setTitle:@"大连" forState:UIControlStateNormal];
//    [city setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    city.titleLabel.font = [UIFont systemFontOfSize:13];
//    [city setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
//    city.imageEdgeInsets = UIEdgeInsetsMake(0, 75, 0, 0);
//    [top addSubview:city];
//    [city release];
    
    self.category = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 150, 0, 115, 35)];
    [_category setTitle:@"降幅最高" forState:UIControlStateNormal];
    [_category setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _category.tag = 1;
    _category.titleLabel.font = [UIFont systemFontOfSize:13];
    _category.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 30);
    [_category setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
    _category.imageEdgeInsets = UIEdgeInsetsMake(0, 85, 0, 0);
    [_category addTarget:self action:@selector(category:) forControlEvents:UIControlEventTouchUpInside];
    [top addSubview:_category];
    [_category release];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, top.frame.origin.y + 35, ScreenWidth, ScreenHeidth - 140) style:UITableViewStylePlain];
    _table.rowHeight = 150;
    [_table registerClass:[PriceOffTableViewCell class] forCellReuseIdentifier:@"a"];
    _table.dataSource = self;
    _table.delegate = self;
    [self.table addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData:)];
    [self.table addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreDate)];
    self.table.footer.automaticallyRefresh = NO;
    [self.view addSubview:_table];
    [_table release];
    
    [SANetWorkingTask request:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=1&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=87dea7fece47586956c66e9e8518a2d3" parmater:nil block:^(NSDictionary *result) {
        [_array removeAllObjects];
        NSDictionary *big = [result objectForKey:@"data"];
        NSArray *arr = [big objectForKey:@"list"];
        [_array addObjectsFromArray:arr];
        [_table.header beginRefreshing];
        
    }];
    

    
}

#pragma mark - 全部车型选择
- (void)carKind:(id)sender
{
    PriceCarViewController *car = [[PriceCarViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:car];
    car.delegate = self;
    [self presentViewController:nav animated:YES completion:nil];
    [nav release];
    [car release];
}

#pragma mark -
#pragma mark 类型选择

- (void)category:(id)sender
{
    UIButton *cate = (UIButton *)sender;

    if (cate.tag == 1) {
        cate.tag = 2;
        cate.layer.borderWidth = 1;
        cate.layer.borderColor = [UIColor lightGrayColor].CGColor;
       
        UIView *ba = [[UIView alloc] initWithFrame:CGRectMake(0, 35, ScreenWidth, ScreenHeidth - 100)];
        ba.tag = 100;
        ba.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:0.4];
        [self.view addSubview:ba];
        [ba release];
        
        UIButton *cate1 = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 150, 35, 115, 30)];
        [cate1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cate1.layer.borderWidth = 1;
        cate1.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cate1.titleLabel.font = [UIFont systemFontOfSize:13];
        cate1.backgroundColor = [UIColor whiteColor];
        [cate1 addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];
        cate1.tag = 101;
        [self.view addSubview:cate1];
        [cate1 release];
        
        UIButton *cate2 = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 150, 65, 115, 30)];
        [cate2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cate2.titleLabel.font = [UIFont systemFontOfSize:13];
        cate2.layer.borderWidth = 1;
        cate2.backgroundColor = [UIColor whiteColor];
        cate2.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cate2.tag = 102;
        [cate2 addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:cate2];
        [cate2 release];
        
        if ([cate.titleLabel.text isEqualToString:@"降幅最高"]) {
            [cate1 setTitle:@"价格最低" forState:UIControlStateNormal];
            [cate2 setTitle:@"价格最高" forState:UIControlStateNormal];
        } else if ([cate.titleLabel.text isEqualToString:@"价格最低"]) {
            [cate1 setTitle:@"降幅最高" forState:UIControlStateNormal];
            [cate2 setTitle:@"价格最高" forState:UIControlStateNormal];
        } else {
            [cate1 setTitle:@"价格最低" forState:UIControlStateNormal];
            [cate2 setTitle:@"降幅最高" forState:UIControlStateNormal];
        }
        
    } else
    {
        cate.tag = 1;
        cate.layer.borderWidth = 0;
        UIButton *cate1 = (UIButton *)[self.view viewWithTag:101];
        [cate1 removeFromSuperview];
        UIButton *cate2 = (UIButton *)[self.view viewWithTag:102];
        [cate2 removeFromSuperview];
        UIView *ba = [(UIView *)self.view viewWithTag:100];
        [ba removeFromSuperview];
        
        
        
    }
    
}

- (void)choose:(id)sender
{
    _category.tag = 1;
    UIButton *chose = (UIButton *)sender;
    NSString *str = _category.titleLabel.text;
    [self.category setTitle:chose.titleLabel.text forState:UIControlStateNormal];
    [chose setTitle:str forState:UIControlStateNormal];
    _category.layer.borderWidth = 0;
    UIButton *cate1 = (UIButton *)[self.view viewWithTag:101];
    [cate1 removeFromSuperview];
    UIButton *cate2 = (UIButton *)[self.view viewWithTag:102];
    [cate2 removeFromSuperview];
    

    
    if ([_category.titleLabel.text isEqualToString:@"降幅最高"]) {
        NSString *str = @"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=1&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=87dea7fece47586956c66e9e8518a2d3";
        
        if ([self.car.text isEqualToString:@"全部车型"]) {

        } else {
        str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=1&pageindex=1&pagesize=20&skip=1&serialid=%@&carid=&token=4eebe67a933d56cdc09602bd89d4dd07", self.serialid];
        }
        [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
            [_array removeAllObjects];
            NSDictionary *big = [result objectForKey:@"data"];
            NSArray *arr = [big objectForKey:@"list"];
            [_array addObjectsFromArray:arr];
            [self.table.header beginRefreshing];
            UIView *ba = [(UIView *)self.view viewWithTag:100];
            [ba removeFromSuperview];

        }];
    } else if ([_category.titleLabel.text isEqualToString:@"价格最高"]) {
        NSString *str = @"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=3&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=e3391040ae644056831be0fe98a678d5";
        
        if ([self.car.text isEqualToString:@"全部车型"]) {
            
        } else {
            str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=3&pageindex=1&pagesize=20&skip=1&serialid=%@&carid=&token=9c91dcb6b30770780aea0bd6d76ad7ae", self.serialid];
        }

        [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
            [_array removeAllObjects];
            NSDictionary *big = [result objectForKey:@"data"];
            NSArray *arr = [big objectForKey:@"list"];
            [_array addObjectsFromArray:arr];
            [self.table.header beginRefreshing];
            UIView *ba = [(UIView *)self.view viewWithTag:100];
            [ba removeFromSuperview];

        }];

    } else {
        NSString *str = @"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=4&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=9999e7409d015fd8d9dcd84dbbb1f0d1";
        
        if ([self.car.text isEqualToString:@"全部车型"]) {
            
        } else {
            str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=4&pageindex=1&pagesize=20&skip=1&serialid=%@&carid=&token=49aa309f52d0c322384df44bbd32f2b2", self.serialid];
        }

        [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
            [_array removeAllObjects];
            NSDictionary *big = [result objectForKey:@"data"];
            NSArray *arr = [big objectForKey:@"list"];
            [_array addObjectsFromArray:arr];
            [self.table.header beginRefreshing];
            UIView *ba = [(UIView *)self.view viewWithTag:100];
            [ba removeFromSuperview];

        }];

    }
    
    
    
    
}


#pragma mark -
#pragma mark 上拉加载,下拉刷新
- (void)loadMoreDate
{
    if ([_category.titleLabel.text isEqualToString:@"降幅最高"]) {
        
        NSString *str = @"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=1&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=87dea7fece47586956c66e9e8518a2d3";
        
        if ([self.car.text isEqualToString:@"全部车型"]) {
            
        } else {
            str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=1&pageindex=1&pagesize=20&skip=1&serialid=%@&carid=&token=87dea7fece47586956c66e9e8518a2d3", self.serialid];
        }

        [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
            [_array removeAllObjects];
            NSDictionary *big = [result objectForKey:@"data"];
            NSArray *arr = [big objectForKey:@"list"];
            [_array addObjectsFromArray:arr];
            [_table reloadData];
            [self.table.footer endRefreshing];
        }];
    } else if ([_category.titleLabel.text isEqualToString:@"价格最高"]) {
        NSString *str = @"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=3&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=e3391040ae644056831be0fe98a678d5";
        
        if ([self.car.text isEqualToString:@"全部车型"]) {
            
        } else {
            str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=3&pageindex=1&pagesize=20&skip=1&serialid=%@&carid=&token=e3391040ae644056831be0fe98a678d5", self.serialid];
        }

        [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
            [_array removeAllObjects];
            NSDictionary *big = [result objectForKey:@"data"];
            NSArray *arr = [big objectForKey:@"list"];
            [_array addObjectsFromArray:arr];
            [_table reloadData];
            [self.table.footer endRefreshing];
        }];
        
    } else {
        NSString *str = @"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=4&pageindex=1&pagesize=20&skip=1&serialid=&carid=&token=9999e7409d015fd8d9dcd84dbbb1f0d1";
        
        if ([self.car.text isEqualToString:@"全部车型"]) {
            
        } else {
            str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/vendor/getpromotionlist/?cityid=1708&sort=4&pageindex=1&pagesize=20&skip=1&serialid=%@&carid=&token=9999e7409d015fd8d9dcd84dbbb1f0d1", self.serialid];
        }
        [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
            [_array removeAllObjects];
            NSDictionary *big = [result objectForKey:@"data"];
            NSArray *arr = [big objectForKey:@"list"];
            [_array addObjectsFromArray:arr];
            [_table reloadData];
            [self.table.footer endRefreshing];
        }];
        
        self.detailsPageCount += 1;
    }

    
    
}

// 下拉刷新
- (void)loadNewData:(id)sender
{
    [self.table reloadData];
    [self.table.header endRefreshing];
}



#pragma mark -
#pragma mark tableview协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PriceOffTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    NSDictionary *dic = [_array objectAtIndex:indexPath.row];
    cell.name.text = [dic objectForKey:@"CarName"];
    cell.DealerName.text = [dic objectForKey:@"DealerName"];
    cell.StoreState.text = [dic objectForKey:@"StoreState"];
    cell.SaleRegion.text = [dic objectForKey:@"SaleRegion"];
    
    cell.ActPrice.text = [NSString stringWithFormat:@"%.2f万", [[dic objectForKey:@"ActPrice"] floatValue]];
    cell.ReferPrice.text = [NSString stringWithFormat:@"%.2f万", [[dic objectForKey:@"ReferPrice"] floatValue]];
    cell.FavPrice.text = [NSString stringWithFormat:@"%@万", [dic objectForKey:@"FavPrice"]];
    [cell.CallCenterNumber setTitle:[dic objectForKey:@"CallCenterNumber"] forState:UIControlStateNormal];
    [cell.CallCenterNumber addTarget:self action:@selector(CallPhone:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *bu = [[UIButton alloc] initWithFrame:CGRectMake(cell.CallCenterNumber.frame.size.width + 10, cell.CallCenterNumber.frame.origin.y, cell.CallCenterNumber.frame.size.width, 35)];
    [bu setTitle:@"询最低价" forState:UIControlStateNormal];
    [bu setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    bu.layer.borderWidth = 1;
    bu.layer.borderColor = [UIColor orangeColor].CGColor;
    bu.titleLabel.font = [UIFont systemFontOfSize:15];
    bu.tag = indexPath.row;
    [bu addTarget:self action:@selector(target:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:bu];
    [bu release];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecSecViewController *web = [[RecSecViewController alloc] init];
    NSDictionary *dic = [_array objectAtIndex:indexPath.row];
    web.str = [dic objectForKey:@"NewsUrl"];
    [self.navigationController pushViewController:web animated:YES];
    [web release];
}


#pragma mark -
#pragma mark 拨打电话
-(void)CallPhone:(UIButton *)sender
{
    NSString *num = sender.titleLabel.text;
    NSString *phoneNum = num;// 电话号码
    
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];
    
    UIWebView *phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    
    [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
    
}



#pragma mark-
#pragma mark 询最低价 跳转
- (void)target:(UIButton *)sender
{
    RecSecViewController *web = [[RecSecViewController alloc] init];
    NSDictionary *dic = [_array objectAtIndex:sender.tag];
    web.str = [dic objectForKey:@"NewsUrl"];
    [self.navigationController pushViewController:web animated:YES];
    [web release];

}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
